<?php
namespace app\mp\admin;
use app\system\admin\Admin;
use app\common\model\Stalls as StallsModel;
use app\common\service\Stalls as StallsService;
class Stalls extends Admin
{
    protected $hisiModel = 'Stalls';//模型名称[通用添加、修改专用]
    protected $hisiValidate = 'Stalls';
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    public function initialize() {
    	parent::initialize();
    }

    public function index()
    {
    	if ($this->request->isAjax()) {
    		$where      = $data = [];
    		$page       = $this->request->param('page/d', 1);
    		$limit      = $this->request->param('limit/d', 20);
    		$keyword    = $this->request->param('keyword/s');

    		if ($keyword) {
    			$where[] = ['name', 'like', "%{$keyword}%"];
    		}

    		$order = 'id desc';
    		$data['data'] = StallsModel::where($where)->page($page)->limit($limit)->order($order)->select();
    		$data['count'] = StallsModel::where($where)->count('id');
    		$data['code'] = 0;
    		$data['msg'] = '';
    		return json($data);
    	}

    	$assign = [];
    	return $this->assign($assign)->fetch();
    }

    //闲置的方法 测试执行结果块使用  sparkguo
    public function test(){
        $materialId=9498;//11888;
        $materialDosageAvg = db('sku_material')->where(['sku_id' => 7260, 'material_id' => $materialId])->value('accurate_dosage', 0);
        echo lastsql();
        print_r($materialDosageAvg);
        $orderId=109303;
        $orderHistory = db('order')->where('sku','swtop07191125911')->where('id', '<', $orderId)->order('id desc')->limit(5)->column('id');
        echo lastsql();
        print_r($orderHistory);
        $materialId=9498;//11888;
        $map = [
            ['material_id', '=', $materialId],
            ['order_id', 'in', $orderHistory],
            ['caichuang_shiji_dosage', '>', 0]
        ];
        $materialDosageArr = db('order_material')->where($map)->column('caichuang_shiji_dosage');
        echo lastsql();echo "<br>";
        if (count($materialDosageArr) >= 3) {
            // 去除最大最小值后取平均值
            $materialDosageAvg = round((array_sum($materialDosageArr)-max($materialDosageArr)-min($materialDosageArr))/(count($materialDosageArr)-2), 2);
            dump($materialDosageAvg);
        } elseif(count($materialDosageArr) > 0) {
            $materialDosageAvgTmp = round(array_sum($materialDosageArr)/count($materialDosageArr), 2);
            if ($materialDosageAvgTmp > 0) $materialDosageAvg = $materialDosageAvgTmp;
        }

        print_r($materialDosageAvg);

    }

	/**
     * 添加供应商
     *
     * @author 飞燕
     * @date 2021年3月17日
     */
	public function add(){
		$name  = $this->request->param('name/s');
		$stallsService= new StallsService();
		$sInfo=$stallsService->getInfoByName($name);
		if($sInfo) return $this->error('此供应商已存在');
		if ($this->request->isPost()) {
			$postData = $this->request->post();
			$postData['create_time']=$postData['update_time']= time();
			$id=db('stalls')->insertGetId($postData);
			if (!$id) {
    			return $this->error('保存失败');
    		}
			$stallsService->updateKVCache();
    		return $this->success('保存成功', '');
			
		}
		$assign = [
    	];
		$template = $this->request->param('template', 'form');
    	return $this->assign($assign)->fetch($template);
	}

    //删除不使用的档口
	public function del(){
        $id  = $this->request->param('id/d');
        $stallsService= new StallsService();
        if(empty($id)){
            return $this->error('缺少必要参数id');
        }
        db('stalls')->delete($id);
        $stallsService->updateKVCache();
        return $this->success('删除成功', '');
    }
}